package hot_100;

public class 环形链表II {
    public static void main(String[] args) {

    }

    public ListNode detectCycle(ListNode head) {
        ListNode show = head;
        ListNode fast = head;
        boolean right = true;
        while (fast != null && fast.next != null) {
            show = show.next;
            fast = fast.next.next;
            if (show == fast) {
                right = false;
                break;
            }
        }
        if (right)
            return null;
        while (show != head) {
            show = show.next;
            head = head.next;
        }
        return show;
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
